
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .message {
                padding: 10px;
                margin-top: 10px;
                border-radius: 10px;
            }

            .self .message {
                background-color: green;
            }

            .target .message {
                background-color: white;
            }

            .message-item-wrapper {
                color: #000000;
            }

            .self .image, .target .image {
                background: none;
            }

            .self {
                display: flex;
                flex-direction: row;
                justify-content: right;
                text-align: right;
            }

            .target {
                display: flex;
                flex-direction: row;
                justify-content: left;
                text-align: left;
            }

            #html  {
                height: 380px;
                overflow-y: scroll;

            }

            .send-message-area {
                width: 100%;
            }

            .send-message-area {
                display: flex;
                flex-direction: row;
                justify-content: center;
                align-items: flex-end;
            }
            #answer {
                width: 100%;
            }

            .button-area {
                display: flex;
                flex-direction: row;
            }

            #sendImage {

            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        
        
        <!-- 数据表格 -->

        <div id="html"></div>

        
        <div class="send-message-area">
            <textarea name="answer" class="layui-textarea" id="answer"></textarea>

            <div class="button-area">
                <div class="layui-btn layui-btn-normal" id="sendImage">图片</div>
                <div class="layui-btn layui-btn-checked" id="sendMsg">发送</div>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.message.insert">
                <i class="layui-icon layui-icon-add-1"></i>新增
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.message.delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.message.update">编辑</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.message.delete">删除</button>
        </script>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            const USER_ID = <?=$userId?>;
            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/message/select?user_id=" + USER_ID;
            const UPDATE_API = "/app/admin/message/update";
            const DELETE_API = "/app/admin/message/delete";
            const INSERT_URL = "/app/admin/message/insert";
            const UPDATE_URL = "/app/admin/message/update";

            let autoScroll = true;

            setInterval(function() {
                getHtml()
            }, 3000);

            function getHtml() {
                let $ = layui.$
                layui.$.ajax({
                    url: '/app/admin/message/getList?user_id=' + USER_ID,
                    dataType: "json",
                        type: "post",
                    success: function(res) {
                        layui.$('#html').html(res.data.data)

                        if (autoScroll) {
                            $('#html').scrollTop($('#html')[0].scrollHeight);
                        }
                    }
                })
            }

            layui.upload.render({
                elem: "#sendImage",
                url: "/app/admin/upload/image",
                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                done: function (res) {
                    sendMsg(2, res.data.url)
                }
            });

            function sendMsg(type, content) {
                layui.$.ajax({
                        url: '/app/admin/message/addMessage',
                        dataType: "json",
                    data: {user_id: USER_ID, type: type, content: content},
                        type: "post",
                        success: function(res) {
                            getHtml()
                        }
                })
            }

            layui.$(function() {
                let $ = layui.$
               layui.$('#sendMsg').click(function() {
                    sendMsg(1, $('#answer').val());
                    $('#answer').val('');
               }) ;

                $('#html').scroll(function() {
                    if ($('#html').scrollTop() + 380 - $('#html')[0].scrollHeight >= -50) {
                        autoScroll = true
                    } else {
                        autoScroll = false
                    }
                })
            });

            getHtml()
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "id",align: "center",
						field: "id",
					},{
						title: "角色，1客服，2用户",align: "center",
						field: "role",
					},{
						title: "用户ID",align: "center",
						field: "user_id",
					},{
						title: "消息类型，1文本消息, 2图片消息",align: "center",
						field: "message_type",
					},{
						title: "消息内容",align: "center",
						field: "content",
					},{
						title: "创建时间",align: "center",
						field: "created_time",
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        shadeClose:true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
            })

        </script>
    </body>
</html>
